/*
 * test.c
 *
 *  Created on: Mar 17, 2015
 *      Author: song
 */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "http_client.h"
#include "pkg_vehicle.h"

static inline long cm_flength(FILE *file)
{
	long pos = ftell(file);
	fseek(file, 0, SEEK_END);
	long len = ftell(file);
	fseek(file, pos, SEEK_SET);

	return len;
}

int main(int argc, char **argv)
{
	if (argc < 2) {
		printf("Usage: %s <JPEG> \n", argv[0]);
		return 0;
	}

	FILE *fp = fopen(argv[1], "r");
	if (fp == NULL) {
		printf("open file(%s) failed.\n", argv[1]);
		return 0;
	}

	size_t flen = (size_t) cm_flength(fp);
	char *data = (char *)malloc(flen);
	if (data == NULL) {
		printf("alloc memory failed.\n");
		fclose(fp);
		return 0;
	}

	int len;
	len = fread(data, 1, flen, fp);
	fclose(fp);

	struct pkg_vehicle pkg;
	memset(&pkg, 0, sizeof(pkg));

	sprintf(pkg.device_id, "TEST_DEVICE");
	pkg.img_num = 1;
	pkg.img_len[0] = len;
	pkg.img[0] = (unsigned char *) data;
	sprintf(pkg.plate, "CA12345");
	pkg.plate_color = 1;
	http_client_setup("192.168.0.171", 8008);
	while (1) {
		http_client_send(&pkg);
		sleep(2);
	}
	free(data);
	return 0;
}
